﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using KingOfCSharp.iHunter;

using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace KingOfCSharp.iHunter.UI
{
    public partial class ChannelSettingsDialog : Form
    {
        private IChannelProvider provider;

        private ChannelInfo settings;

        private UserControl customPage;

        private bool updateTimeChanged = false;

        private LocalClient client;

        public ChannelSettingsDialog(LocalClient client, IChannelProvider provider, ChannelInfo settings)
        {
            InitializeComponent();
            this.client = client;
            commonPage.Client = client;

            commonPage.dateTimeLastUpdate.ValueChanged += new EventHandler(dateTimeLastUpdate_ValueChanged);

            this.provider = provider;
            ChannelSettings = settings;
        }

        void dateTimeLastUpdate_ValueChanged(object sender, EventArgs e)
        {
            updateTimeChanged = true;
        }

        private void ChannelSettingsDialog_Load(object sender, EventArgs e)
        {
            customPage = provider.GetChannelConfigureEditor();
            customPage.Dock = DockStyle.Fill;
            this.tabPageCustom.Controls.Add(customPage);
    
            /*
            commonPage.ChannelName = settings.Name;
            commonPage.CheckExists = settings.RefreshExists;
            commonPage.SelectedFolderGuid = settings.FolderGuid;
            commonPage.LastUpdateTime = settings.UpdateTime;
             */
            IXmlSerializable ser = customPage as IXmlSerializable;
            using (TextReader rd = new StringReader(settings.Settings))
            using (XmlReader xreader = XmlReader.Create(rd))
            {
                ser.ReadXml(xreader);
            }
        }

        public ChannelInfo ChannelSettings
        {
            get
            {
                return settings;
            }
            set
            {
                settings = value;
                commonPage.ResetControlValues(settings);
            }
        }

        private bool ValidateChannelCommonSettings()
        {
            if (commonPage.ChannelName == string.Empty)
            {
                MessageBox.Show("You should specify a name for this Channel.", "Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (!commonPage.folderTreeView.IsSelectedFolderChildOf(client.Inbox))
            {
                MessageBox.Show("You must select a folder under Inbox.", "Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            return true;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (!ValidateChannelCommonSettings())
            {
                return;
            }

            settings.Name = commonPage.ChannelName;
            settings.RefreshExists = commonPage.CheckExists;
            settings.FolderGuid = commonPage.TargetFolderGuid;
            if (updateTimeChanged)
            {
                DateTime tt = commonPage.LastUpdateTime;
                settings.UpdateTime = new DateTime(tt.Year, tt.Month, tt.Day);
            }

            StringBuilder buf = new StringBuilder();

            try
            {
                XmlWriter wt = XmlWriter.Create(buf);

                IXmlSerializable ser = customPage as IXmlSerializable;

                ser.WriteXml(wt);

                wt.Close();

                settings.Settings = buf.ToString();
            }
            catch (ArgumentException)
            {
                return;
            }

            this.DialogResult = DialogResult.OK;
        }
    }
}
